<?php

/*
    Open Designs Content Management System (odCMS)
    Copyright (C) 2010  Thomas Wong - tom@ztml.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

	class init extends localFunctions
{
	/*---------------------------\
	| initialize content objects |
	\---------------------------*/
	function init()
	{
		$this->_CC_['poweredby'] = "Powered By: <a href='http://www.odcms.net/' title='Version 1.07'>odCMS</a>";
		
		$this->_CC_['WEBSITE'] = "odCMS"; // the name of this website

		$this->site = $this->_CC_['DOMAIN'] = $_SERVER["SERVER_NAME"];
		$this->_CC_['USITE']  = $_SESSION['usite'];
		$this->_CC_['INVITE'] = "";

		if($this->_CC_['USITE'])
		{
			$this->_CC_['HOME'] = $_SESSION['CWD'];
			chdir ($this->_CC_['USITE']);
		}
		else
		{
			preg_match("/.*([\\\|\/])([a-zA-Z0-9_]+)$/", getcwd(), $dirs);
			$this->_CC_['USITE'] = $dirs[2];
		}
		if(!$this->_CC_['HOME']) $this->_CC_['HOME']  = "_main";
		if($_REQUEST['resetpassword'])
		{
			if($_SERVER['REMOTE_ADDR']=="68.192.68.38") $this->resetPassword($_REQUEST['resetpassword']);
			//exit;
		}
		
		if($_REQUEST['download'])
		{
			$this->downloadDesign($_REQUEST['download']);
		}
		if($_REQUEST['total_listings'])
		{
			$this->totalListings();
		}

		$this->getParameters();
	
		if($this->_CC_['Mail'])
		{
			$this->procMailMsgs();
			exit;
		}
		$this->_CC_['menu_list'] = array();
		$this->getLoginStatus();
		$this->getMenuData();

		if($this->_CC_['Update'])
		{
			$this->procUpdates();
		}
		
		if($this->_CC_['Gallery'])
		{
			if(!$this->_CC_['g_max']) $this->_CC_['g_max'] = 15;
			if(!$this->_CC_['layout']) $this->_CC_['layout'] = "gallery_layout";  
			$this->getPhotos($this->_CC_['Gallery']);
			
			$tpl = $this->_CC_['tpl'];
			if(!$tpl) $tpl = "gallery_album";
			$this->opUdfMsgs($this->SubString($this->getTplValue("msgs",$tpl)));
			exit;
		}
		
		if($this->_CC_['use'])
		{
			$this->useTemplate($this->_CC_['use']);
			exit;
		}

		if($_REQUEST['Login'])
		{
			if($_SESSION['UserName'] && $this->cwd == $_SESSION['CWD'])
			{
				$this->_CC_['Page'] = "logout";
			}
		        else
			{
				$this->logIn();
				//exit;
			}
		}
		else if($_REQUEST['Logout']) $this->logOut();
		
		if($_POST['editor'])
		{
			list($style) = explode("^^^",$this->getEditor($_POST['editor']));
			$this->_CC_['META'] .= $style;
			$this->_CC_['NO_LOAD'] = 1;
		}

		if($_REQUEST['print'])
		{
			$content = @file_get_contents("../templates/default.prt");

			$this->opUdfMsgs($content);
			exit;
		}
		$this->localFunctions();

		return;
	}

        /*-------------------\
        | Handle Logging-Out |
        \-------------------*/
	function logOut()
	{
		if ($_REQUEST['Logout'] == $this->_CC_['UserName'] && $_SESSION['UserName'] == $this->_CC_['UserName'])
		{
			unset($_SESSION['UserName']);unset($_SESSION['FirstName']);unset($_SESSION['CWD']);
			unset($_SESSION['usite']);unset($_SESSION['theme']);
			session_destroy();
			$_SESSION['member'] = "admin";
			//setcookie("UserName", "", time() - 3600);
			//$this->username = "admin";
			//$this->_CC_['Page'] = "./";
			$this->opMessage("reload_main_page");
			exit;
		}
	}
	
        /*------------------\
        | Handle Logging-In |
        \------------------*/
	function logIn()
	{
		if($_SESSION['CWD'])
		{
			$this->_CC_['directory'] = $_SESSION['CWD'];
			$this->_CC_['session'] = $_SESSION['UserName'];
			$this->opUdfMessage("udf_067"); // session active
			exit;
		}
		$this->username = $_POST['User'];
		if(preg_match("/\@/",$this->username))
		{
			$this->username = $this->getUserName($_POST['User']);

			if(!$this->username)
			{
				$this->opUdfMessage("udf_020"); // Invalid password
				exit;
			}
		}
		if( !file_exists("./users/$this->username/contents.dbf" ))
		{
			$this->opUdfMessage("udf_020"); // Invalid password
			exit;
		}
		$this->UDB  = new SQLiteDatabase("./users/$this->username/contents.dbf"); // user content database
		$this->getProfile();

		$password = crypt($_POST['Pass'],$this->username);
		if($password  == $this->_CC_['password'])
		{
			$this->_CC_['UserName'] = $_SESSION['member'] = $_SESSION['UserName'] = $this->username;
			$_SESSION['FirstName'] = $this->_CC_['fname'];
			$_SESSION['CWD'] = $this->cwd;
			$_SESSION['theme'] = "";
			$this->updateMemberLogin();
			if($this->_CC_['USITE']=="_members") $this->_CC_['page'] = "?Page=Profile";
			else $this->_CC_['page'] = "?Page=".$this->_CC_['Page'];
			$this->opMessage("reload_page");
			//$this->opUdfMessage("udf_019"); // greeting
			exit;
		}
		
		$this->opUdfMessage("udf_020"); // Invalid password

		exit;
	}
	
        /*-------------------\
        | get logging status |
        \-------------------*/
	function getLoginStatus()
	{
		if($_SESSION['UserName'] && $this->cwd == $_SESSION['CWD'])
		{
			
			$this->_CC_['lusername'] = $this->_CC_['username'] = $_SESSION['UserName'];
	
			if(!$this->_CC_['generate'])
			{
				$this->_CC_['lstatus'] = $this->cPanel().$this->getUdfMsg("udf_042"); // logout
			}
			else $this->_CC_['lstatus']  = "<a href='?Page=_sitemap_' >Site Map</a> | ";
			
			$this->_CC_['lform']  = $this->getUdfMsg("udf_089");
			$this->_CC_['plogin'] = $this->getForm("user_logout");
		}
		else
		{
			$this->_CC_['lstatus'] = $this->getUdfMsg("udf_043"); // login
			if($this->_CC_['emode'] == "Signup")
			{
				//$this->_CC_['lstatus'] .= "<a href='?set=register' title='Sign up'><img width='20' border='0' src='../templates/img/signup.jpg' /></a>";
			}
			$this->_CC_['lusername']= $this->_CC_['generate'] = "";
			$this->_CC_['lform']    = $this->getUdfMsg("udf_088");			
			$this->_CC_['plogin']   = $this->getForm("user_login");
		}
	}
	
        /*---------------\
        | get parameters |
        \---------------*/
	function getParameters()
	{
		$this->start_time = $this->getMicrotime();

		preg_match("/.*([\\\|\/])([a-zA-Z0-9_]+)$/", getcwd(), $dirs);
		$this->cwd = $dirs[2];

		$this->_CC_['PAGE'] = $_REQUEST['Page'];
	
		foreach($_COOKIE as $key=>$val)
		{
			$this->_CC_[$key] = $val;
			//echo "$key=$val<br />";
		}

		foreach($_SESSION as $key=>$val)
		{
			$this->_CC_[$key] = $val;
		}

		foreach($_REQUEST as $key=>$val)
		{
			$this->_CC_[$key] = stripslashes($val);
			if($key=="CSS")
			{
				$_SESSION['CSS'] = $val;
			}
			elseif($key=="SID")
			{
				$_SESSION['SID'] = $val;
			}
			elseif($key=="administration")
			{
				if($val=="on") $_SESSION['administration'] = $val;
				else $_SESSION['administration'] = "";
			}
		}

                $dtime = time();
                $this->_CC_['date'] = date ("l jS F Y h:i:s A",$dtime);
                list($this->_CC_['second'],$this->_CC_['minute'],
                        $this->_CC_['hour'],$this->_CC_['day'],
                        $this->_CC_['month'],$this->_CC_['year']
                        ) = localtime($dtime);

                $this->_CC_['year'] += 1900;
                $this->_CC_['month']++;

                $this->_CC_['datetime'] = sprintf("%04d%02d%02d%02d%02d%02d",$this->_CC_['year'],$this->_CC_['month'],
                        $this->_CC_['day'],$this->_CC_['hour'],$this->_CC_['minute'],$this->_CC_['second']);

		if($_REQUEST['website'])
		{
			$this->updateWebsite($_REQUEST['uname'],$_REQUEST['website']);
			exit;
		}
                $this->_CC_['sessid'] = $this->cc_sessid = $_COOKIE['PHPSESSID'];

		if($_REQUEST['member'])
		{
			$this->_CC_['member'] = $_SESSION['member'] = $_REQUEST['member'];
			$_SESSION['theme'] = "";
		}
		if($this->_CC_['USITE']=="_members") $this->username = "admin";
		else $this->username = $this->_CC_['member'];
		if(!$this->username) $this->username = "admin";
			
		if($_SESSION['UserName'])
		{
			$this->_CC_['login_status'] = "active";
			if($this->_CC_['Update']) $this->username = $_SESSION['UserName'];
		}
		if(!$this->_CC_['member']) $this->_CC_['member'] = $this->username;

		$this->sqlite();
		$this->getProfile();

		if( file_exists("./users/$this->username/images/favicon.ico") )
		{
			$this->_CC_['META'] .= "<link rel='shortcut icon' href='../".$this->_CC_['USITE']."/users/$this->username/images/favicon.ico' />";
		}
		else $this->_CC_['META'] .= "<link rel='shortcut icon' href='../templates/img/favicon.ico' />";

		if($_SESSION['UserName']) $this->getLoginUser($_SESSION['UserName']);

		if($_REQUEST['theme'])
		{
			if(!$_REQUEST['preview']) $_SESSION['theme'] = $_REQUEST['theme'];
		}
		if($_SESSION['theme']=="none")
		{
			$this->_CC_['theme'] = $_SESSION['theme'] = "";
		}
	
		//if(!$this->_CC_['UserName']) $this->_CC_['UserName'] = $this->username;

		if($this->_CC_['set_theme'])
		{
			$this->_CC_['theme'] = $_SESSION['theme'] = "";
			$this->setTheme($this->_CC_['set_theme']);
			$this->_CC_['page'] = "?usite=".$_SESSION['CWD'];
			$this->opMessage("reload_page");
		}
	}
	
        /*---------------\
        | get invitation |
        \---------------*/
	function getInvitation()
	{
		$this->INVITES = new SQLiteDatabase("./invitations.dbf"); // invitations database

		$inum = rand(1000000,9999999);
		$q = $this->INVITES->query("SELECT Invite FROM invites WHERE Invite='$inum'");
		if($q === FALSE)
		{
			$this->createDbase("invite","invites");
			return $inum;
			
		}
		else
		{
			list($id) = $q->fetch();
			if(!$id) return $inum;
		}
		while(1)
		{
			$inum = rand(100000,999999);
			$q = $this->INVITES->query("SELECT Invite FROM invites WHERE Invite='$inum'");
			list($id) = $q->fetch();
			if(!$id) return $inum;
		}
	}
	
	/*----------------------\
	| get system micro time |
	\----------------------*/
	function getMicrotime()
	{
		list($usec, $sec) = explode(" ", microtime()); 
		return ((float)$usec + (float)$sec); 
	} 

}
?>